//模块配置信息
require.config({
	//baseUrl: "./",
	paths: {
		//工具模块
		'less': 'tools/less.min',
		'jquery': 'tools/jquery.min',
		'bootstrap': 'tools/bootstrap.min',
		'handlebars': 'tools/handlebars',

		//配置模块
		'config': './config',
		"appConfig":"./appConfig",

		'top_bar': 'modules/top_bar',
		'problem': 'modules/problem',
		'ask': 'modules/ask',
		'modal_template': 'modules/modal_template',
		'cookie': 'modules/cookie',
		'check_log': 'modules/check_log',
		'login': 'modules/login',
		'other_log': 'modules/login',
		'reg': 'modules/reg',
		'user': 'modules/user',
		'userProblem':'modules/userProblem'　　
	},
	skim: {  //配置依赖关系与接口
		"bootstrap": {	// bootstrap依赖jquery库
			deps: ['jquery'],
			exports: 'bootstrap'
		},
		"handlebars": {
			exports: 'Handlebars'
		},
		"user": {
			deps: ['modal_template']
		}
	}
});
/************************************header***************************************/
//加载顶栏top_bar模块
require(['top_bar'], function(top_bar){
	top_bar();
});
/************************************content***************************************/
require(['jquery','handlebars','cookie','config'], function ($,Handlebars,cookie,config){
	require(['bootstrap','less']);
	var userId=cookie.getCookie("userId");
	if(userId==""){
		alert("请先登录");
		return ;
	}

	function fetchFollowList(){
		$.ajax({
			url:config.api.getFollows[0].replace(/\<\w+\>/,userId),
			type:config.api.getFollows[1],
			success:function(d){
				handlebarsRender("#followList-template",".followListContainer",d);
				$(".userList").on("click",".cancelFollowBtn",function(e){
					e.stopPropagation();
					var id=$(this).closest(".userItem").data("userid");
					var utype=$(this).closest(".userItem").data("utype");
					var that=this;
					if(utype==2){//画室
						cancelFollowStudio(id,function(){
							$(that).text("关注")//修改按钮文字
								   .removeClass("cancelFollowBtn")
								   .addClass("FollowBtn");//修改类
						});
					}else{
						cancelFollowUser(id,function(){
							$(that).text("关注")//修改按钮文字
								   .removeClass("cancelFollowBtn")
								   .addClass("FollowBtn");//修改类
						});
					}

				});
				$(".userList").on("click",".FollowBtn",function(e){
					e.stopPropagation();
					var id=$(this).closest(".userItem").data("userid");
					var utype=$(this).closest(".userItem").data("utype");
					var that=this;
					if(utype==2){//画室
						followStudio(id,function(){
							$(that).text("互相关注")//修改按钮文字
								   .removeClass("FollowBtn")
								   .addClass("cancelFollowBtn");//修改类
						});
					}else{
						followUser(id,function(){
							$(that).text("互相关注")//修改按钮文字
								   .removeClass("FollowBtn")
								   .addClass("cancelFollowBtn");//修改类
						});
					}

				});
				$(".userList").on("click",".userItem",function(){
					var id=$(this).data("userid");
					var utype=$(this).data("utype");
					if(utype==2){
						window.location.href="studio.html?studioId="+id;
					}else{
						window.location.href="otherInfo.html?userId="+id;
					}
					
				});
			}
		})
	}
	function fetchUserData(){
		$.ajax({
			url:config.api.getUserInfo[0].replace(/\<\w+\>/,userId),
			type:config.api.getUserInfo[1],
			success:function(d){
				handlebarsRender("#userInfo-template",".socailInfoContainer",d);
			},
			error:function(){
				alert("获取数据出错");
			}
		});
	}
	function cancelFollowUser(idToCancel,callback){
		$.ajax({
			url:config.api.followUser[0].replace(/\<\w+\>/,userId),
			type:config.api.followUser[1],
			data:{
				userId:idToCancel,
				follow:0
			},
			success:function(d){
				alert("取消关注成功");
				if(typeof callback=="function"){
					callback();
				}
			},
			error:function(){
				alert("取消关注失败");
			}
		});
	}
	function cancelFollowStudio(idToCancel,callback){
		$.ajax({
			url:config.api.cancelSubscribeStudio[0].replace(/\<\w+\>/,userId),
			type:config.api.cancelSubscribeStudio[1],
			data:{
				studioId :idToCancel,
			},
			success:function(d){
				alert("取消关注成功");
				if(typeof callback=="function"){
					callback();
				}
			},
			error:function(){
				alert("取消关注失败");
			}
		});
	}
	function followUser(idToFollow,callback){
		$.ajax({
			url:config.api.followUser[0].replace(/\<\w+\>/,userId),
			type:config.api.followUser[1],
			data:{
				userId:idToFollow,
				follow:1
			},
			success:function(d){
				alert("关注成功");
				if(typeof callback=="function"){
					callback();
				}
			},
			error:function(){
				alert("关注失败");
			}
		});
	}
	function followStudio(idToFollow,callback){
		$.ajax({
			url:config.api.subscribeStudio[0].replace(/\<\w+\>/,userId),
			type:config.api.subscribeStudio[1],
			data:{
				studioId:idToFollow,
			},
			success:function(d){
				alert("关注成功");
				if(typeof callback=="function"){
					callback();
				}
			},
			error:function(){
				alert("关注失败");
			}
		});
	}
	function handlebarsRender(tplSelector, destSelector, data) {
		//用jquery获取模板
		var source = $(tplSelector).html();
		//预编译模板
		var template = Handlebars.compile(source);
		//匹配json内容
		var html = template(data);
		//输入模板
		$(destSelector).html(html);
	}
	Handlebars.registerHelper("followEachOther",function(relation,options){

		if(relation==3){
			return options.fn(this);
		}else{
			return options.inverse(this);
		}
	});//注册一个判断是否相互关注的helper
	fetchFollowList();
	fetchUserData();
	
});
